/*
 * @auther LightweightCoder
 */

package book;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Side;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import model.Book;
import model.Item;
import model.Publisher;


/**
 *
 * @author Lenovo
 */
public class AppleBookStore extends Application {
    final ObservableList<Book> data1 = FXCollections.observableArrayList(
            new Book("B001", 10, 50.0, 60.0, Item.Status.AVAILABLE, 5, "Test1", "Auth1", new Publisher("comp1", "addr1"), "IS12", "ISN12", Book.Fiction.SCIENCE),
            new Book("B002", 10, 50.0, 60.0, Item.Status.AVAILABLE, 5, "Test2", "Auth2", new Publisher("comp1", "addr1"), "IS13", "ISN13", Book.Fiction.SCIENCE)
        );
    public static void main(String[] args) {
        launch(args);
    }
 
    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Apple Book Store");
        stage.setWidth(1080);
        stage.setHeight(500);
        final GridPane mainGrid = new GridPane();
        mainGrid.add(getHeaderLabel(), 0, 0);
        mainGrid.add(genTabPane(), 0, 1);
        ((Group) scene.getRoot()).getChildren().addAll(mainGrid);
 
        stage.setScene(scene);
        stage.show();
    }
    
    public HBox getBtnBox(){
        // Button box for Book Tab
        final HBox btnBox = new HBox();
        final Button addBtn = new Button();
        addBtn.setText("Add");
        final Button updBtn = new Button();
        updBtn.setText("Update");
        final Button delBtn = new Button();
        delBtn.setText("Inactive");
        final Button goBtn = new Button();
        goBtn.setText("Go");
        final Button resetBtn = new Button();
        resetBtn.setText("Reset");
        final Label serLable = new Label("Search");
        final TextField serText = new TextField();
        serText.setPromptText("Search Book");
        btnBox.setSpacing(10);
        btnBox.setPadding(new Insets(5));
        btnBox.getChildren().addAll(addBtn, updBtn, delBtn, serLable,serText,goBtn,resetBtn);
        return btnBox;
    }
    
    public VBox genVbox(){
        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        final Label label = new Label("Book List");
        label.setFont(new Font("Arial", 20));
        vbox.getChildren().addAll(label, generateBookTable());
        return vbox;
    }
    
    public GridPane addBookForm(){
        GridPane formGrid = new GridPane();
        formGrid.setPadding(new Insets(20));
        
        final Label itemCode = new Label("Item Code :");
        
        final Label qty = new Label("Quantity :");
        final Label cost = new Label("Cost price :");
        final Label sell = new Label("Seling Price :");
        final Label status = new Label("Status :");
        final Label discount = new Label("Discount :");
        final Label title = new Label("Title :");
        final Label auth = new Label("Author :");
        final Label isbn10 = new Label("ISBN10 :");
        final Label isbn13 = new Label("ISBN13 :");
        final Label fiction = new Label("Fiction :");
        final Label blank = new Label("");
        
        final TextField itmText = new TextField();
        final TextField qtyText = new TextField();
        final TextField costText = new TextField();
        final TextField sellText = new TextField();
        final TextField statusText = new TextField();
        final TextField disText = new TextField();
        final TextField titText = new TextField();
        final TextField authText = new TextField();
        final TextField isbn10Text = new TextField();
        final TextField isbn13Text = new TextField();
        final TextField fictionText = new TextField();
        
        final HBox box2 = new HBox();
        final Button saveBtn = new Button("Save");
        final Button canBtn = new Button("Cancel");
        box2.setSpacing(10);
        box2.setPadding(new Insets(10));
        box2.getChildren().addAll(saveBtn,canBtn);
        
        formGrid.add(itemCode, 0, 0);
        formGrid.add(qty, 0, 1);
        formGrid.add(cost, 0, 2);
        formGrid.add(sell, 0, 3);
        formGrid.add(status, 0,4);
        formGrid.add(discount, 0, 5);
        formGrid.add(title, 0, 6);
        formGrid.add(auth, 0, 7);
        formGrid.add(isbn10, 0, 8);
        formGrid.add(isbn13, 0, 9);
        formGrid.add(fiction, 0, 10);
        formGrid.add(blank, 0, 11);
        
        formGrid.add(itmText, 1, 0);
        formGrid.add(qtyText, 1, 1);
        formGrid.add(costText, 1, 2);
        formGrid.add(sellText, 1, 3);
        formGrid.add(statusText, 1, 4);
        formGrid.add(disText, 1, 5);
        formGrid.add(titText, 1, 6);
        formGrid.add(authText, 1, 7);
        formGrid.add(isbn10Text, 1, 8);
        formGrid.add(isbn13Text, 1, 9);
        formGrid.add(fictionText, 1, 10);
        formGrid.add(box2, 1, 11);
        
        saveBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                data1.add(new Book("B001", 10, 50.0, 60.0, Item.
                            Status.AVAILABLE, 5, "Test1", "Auth1", 
                            new Publisher("comp1", "addr1"), "IS12", "ISN12", 
                            Book.Fiction.SCIENCE));
               /* data1.add(new Book(
                        itmText.getText(),
                        5,
                        10,
                        15,
                        statusText.getText(),
                        disText.getText(),
                        titText.getText(),
                        authText.getText(),
                        new Publisher("comp1", "addr1"),
                        isbn10Text.getText(),
                        isbn13Text.getText(),fictionText.getText()
                        ));
                itmText.clear();
                qtyText.clear();
                costText.clear();
                sellText.clear();
                statusText.clear();
                disText.clear();
                titText.clear();
                authText.clear();
                isbn10Text.clear();
                isbn13Text.clear();
                fictionText.clear();*/
            }  
        });
        
        canBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                itmText.clear();
                qtyText.clear();
                costText.clear();
                sellText.clear();
                statusText.clear();
                disText.clear();
                titText.clear();
                authText.clear();
                isbn10Text.clear();
                isbn13Text.clear();
                fictionText.clear();
            }   
        });
       
        return formGrid;
        
        
    }
    
    public GridPane getBookGrid(){
        final GridPane bookGrid = new GridPane();
        bookGrid.add(getBtnBox(), 0, 0);
        bookGrid.add(genVbox(),0,1);
        return bookGrid;
    }
    
    public GridPane getAddGrid(){
        HBox box1 = new HBox();
        box1.setPadding(new Insets(250,0,0,30));
        GridPane addBGrid = new GridPane();
        addBGrid.setPadding(new Insets(50));
        addBGrid.add(addBookForm(), 0, 0);
        return addBGrid;
    }
    
    public HBox genBookBox(){
        final HBox bookBox = new HBox();
        bookBox.getChildren().addAll(getBookGrid(),getAddGrid());
        return bookBox;
    }
    
    public HBox getHeaderLabel(){
        final HBox headerbox = new HBox();
        headerbox.setAlignment(Pos.CENTER);
        
        final Label headlabel = new Label("Apple Book Store");  
        headlabel.setFont(new Font("System Bold", 32));
        headlabel.setTextFill(Color.web("#f9fafc"));
        
        headerbox.setPadding(new Insets(15, 12, 15, 12));
        headerbox.setSpacing(10);
        headerbox.setStyle("-fx-background-color: #336699;");
        headerbox.getChildren().addAll(headlabel); 
        return headerbox;
    }
    
    public TabPane genTabPane(){
        TabPane tabPane = new TabPane();
        tabPane.setSide(Side.BOTTOM);
        tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
        Tab bookTab =new Tab();
        bookTab.setText("Book");
        Tab cdTab =new Tab();
        cdTab.setText("CD");
        Tab stTab =new Tab();
        stTab.setText("Stationary");
        
        bookTab.setContent(genBookBox());
        
        tabPane.getTabs().add(bookTab);
        tabPane.getTabs().add(cdTab);
        tabPane.getTabs().add(stTab);
        return tabPane;
    }
    
    public TableView<Book> generateBookTable(){
        TableView<Book> bookTable = new TableView<Book>();
        bookTable.setMaxHeight(500);
        bookTable.setMaxWidth(1000);

        
        bookTable.setEditable(true);
        bookTable.setTableMenuButtonVisible(true);
 
        TableColumn itemCode = new TableColumn("Item Code");
        itemCode.setMinWidth(100);
        itemCode.setCellValueFactory(new PropertyValueFactory<Item,String>("itemCode"));
        
        TableColumn qty = new TableColumn("Quantity");
        qty.setMinWidth(100);
        qty.setCellValueFactory(new PropertyValueFactory<Item,String>("quantityInHand"));
        
        TableColumn costPrice = new TableColumn("Cost Price");
        costPrice.setMinWidth(100);
        costPrice.setCellValueFactory(new PropertyValueFactory<Item,String>("costPrice"));
        
        TableColumn sellPrice = new TableColumn("Selling Price");
        sellPrice.setMinWidth(100);
        sellPrice.setCellValueFactory(new PropertyValueFactory<Item,String>("sellingPrice"));
        
        TableColumn status = new TableColumn("Status");
        status.setMinWidth(100);
        status.setCellValueFactory(new PropertyValueFactory<Item,String>("status"));
        
        TableColumn discount = new TableColumn("Discount");
        discount.setMinWidth(100);
        discount.setCellValueFactory(new PropertyValueFactory<Item,String>("discount"));
        
        TableColumn bookTitle = new TableColumn("Title");
        bookTitle.setMinWidth(100);
        bookTitle.setCellValueFactory(new PropertyValueFactory<Item,String>("bookTitle"));
        
        TableColumn author = new TableColumn("Author");
        author.setMinWidth(100);
        author.setCellValueFactory(new PropertyValueFactory<Item,String>("author"));
        
        TableColumn publisher = new TableColumn("Publisher");
        publisher.setMinWidth(100);
        publisher.setCellValueFactory(new PropertyValueFactory<Item,String>("publisher.companyName"));
        
        TableColumn ISBN10 = new TableColumn("ISBN10");
        ISBN10.setMinWidth(100);
        ISBN10.setCellValueFactory(new PropertyValueFactory<Item,String>("ISBN10"));
        
        TableColumn ISBN13 = new TableColumn("ISBN13");
        ISBN13.setMinWidth(100);
        ISBN13.setCellValueFactory(new PropertyValueFactory<Item,String>("ISBN13"));
        
        TableColumn fiction = new TableColumn("Fiction");
        fiction.setMinWidth(100);
        fiction.setCellValueFactory(new PropertyValueFactory<Item,String>("fiction"));
        
        bookTable.setItems(data1);
        bookTable.getColumns().addAll(itemCode, qty, costPrice, sellPrice, status, discount,bookTitle,author, publisher,ISBN10,ISBN13,fiction);
        
        return bookTable;
    }
 
    
} 
    

